﻿using Coldairarrow.Entity;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business
{
    public class Invoice_InfoBusiness : BaseBusiness<Invoice_Info>, IInvoice_InfoBusiness, ITransientDependency
    {
        readonly Operator _operator;
        public Invoice_InfoBusiness(IDbAccessor db, Operator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<Invoice_Info>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Invoice_Info>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Invoice_Info, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }


        /// <summary>
        /// 获取个人发票
        /// </summary>
        /// <returns></returns>
        public async Task<List<Invoice_Info>> GetMyInvoce()
        {
            var sList = await GetIQueryable().Where(x => x.CreatorId.Equals(_operator.UserId)).OrderByDescending(x => x.CreateTime).ToListAsync();

            return sList;
        }

        public async Task<Invoice_Info> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }
        public List<Invoice_Info> GetInvoice(string Phone)
        {
            return GetIQueryable().WhereIf(!Phone.IsNullOrEmpty(),q => q.Phone.Contains(Phone)).Distinct().ToList();
        }
        public async Task AddDataAsync(Invoice_Info data)
        {
            var sCount = 0;
            if (data.Rise_Type == 0)
            {
                data.Invoice_Type = 0;
            }

            if (data.Rise_Type == 1)
            {
                sCount = GetIQueryable().Where(x => x.Invoice_Type.Equals(data.Invoice_Type) && x.Rise_Type.Equals(data.Rise_Type) && x.Phone.Equals(data.Phone) && x.Unit_Phone.Equals(data.Unit_Phone) && x.UnitName.Equals(data.UnitName) && x.UnitTax.Equals(data.UnitTax)).Count();
            }
            else
            {
                sCount = GetIQueryable().Where(x => x.Invoice_Type.Equals(data.Invoice_Type) && x.Rise_Type.Equals(data.Rise_Type) && x.Phone.Equals(data.Phone) && x.PerName.Equals(data.PerName)).Count();
            }

            if (sCount > 0)
            {
                throw new BusException("此订单发票已存在，创建失败！");
            }

            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Invoice_Info data)
        {
            var sCount = 0;
            if (data.Rise_Type == 0)
            {
                data.Invoice_Type = 0;
            }

            if (data.Invoice_Type == 1)
            {
                sCount = GetIQueryable().Where(x => x.Id != data.Id && x.Invoice_Type.Equals(data.Invoice_Type) && x.Rise_Type.Equals(data.Rise_Type) && x.Phone.Equals(data.Phone) && x.Emails.Equals(data.Emails) && x.Accont.Equals(data.Accont) && x.Unit_Address.Equals(data.Unit_Address) && x.Unit_Phone.Equals(data.Unit_Phone)).Count();
            }
            else
            {
                sCount = GetIQueryable().Where(x => x.Id != data.Id && x.Invoice_Type.Equals(data.Invoice_Type) && x.Rise_Type.Equals(data.Rise_Type) && x.Phone.Equals(data.Phone) && x.Emails.Equals(data.Emails)).Count();
            }

            if (sCount > 0)
            {
                throw new BusException("此订单发票已存在，修改失败！");
            }

            var model = GetEntity(data.Id);
            model.Invoice_Type = data.Invoice_Type;
            model.Rise_Type = data.Rise_Type;
            model.Phone = data.Phone;
            model.Emails = data.Emails;
            model.Accont = data.Accont;
            model.DATA0 = data.DATA0;
            model.UnitName = data.UnitName;
            model.UnitTax = data.UnitTax;
            model.PerName = data.PerName;
            model.Unit_Address = data.Unit_Address;
            model.Unit_Phone = data.Unit_Phone;

            await UpdateAsync(model);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<Invoice_Info> AddDataTwoAsync(Invoice_Info data)
        {
            int t = await InsertAsync(data);
            return data;
        }

        #endregion 外部接口
    }
}